package com.crr.mall.product.service.impl;

import com.crr.mall.common.utils.Assert;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crr.mall.common.utils.PageUtils;
import com.crr.mall.common.utils.Query;

import com.crr.mall.product.dao.SpuAttrValueDao;
import com.crr.mall.product.entity.SpuAttrValueEntity;
import com.crr.mall.product.service.SpuAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("spuAttrValueService")
public class SpuAttrValueServiceImpl extends ServiceImpl<SpuAttrValueDao, SpuAttrValueEntity> implements SpuAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuAttrValueEntity> page = this.page(
                new Query<SpuAttrValueEntity>().getPage(params),
                new QueryWrapper<SpuAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SpuAttrValueEntity> getBySpuId(Long spuId) {
        return baseMapper.getBySpuId(spuId);
    }

    @Transactional
    @Override
    public int updateBySpuId(Long spuId, List<SpuAttrValueEntity> list) {
        //根据spuId批量删除
        int i =baseMapper.deleteBySpuId(spuId);
        Assert.fail(i>=1,"批量删除失败");

        //批量增加
        list.forEach(l-> {
            l.setSpuId(spuId);
        });
        baseMapper.insert(list);
        return 1;
    }


}